﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;
using MINERP.DAL;

namespace MINERP.BLL.NewPhotoRindGrid
{
    // 处理与分区相关的逻辑类
    public class SubAreaBLL
    {
        public List<GridSubArea_Info> GetAllSubAreaByType(SubAreaType type)
        {
            int subType = (int)type;
            List<GridSubArea_Info> result = null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var s in db.GridSubArea.Where(s => s.Type == subType && s.isUseful).ToList())
                {
                    if (result == null)
                    {
                        result = new List<GridSubArea_Info>();
                    }
                    result.Add(new GridSubArea_Info()
                    {
                        SubAreaID = s.SubAreaID,
                        GridClearTime = s.GridClearTime,
                        HeightNum = s.heightNum,
                        OutTimeAlarmTime = s.OutTimeAlarmTime == null ? 0 : (double)s.OutTimeAlarmTime,
                        SubAreaName = s.SubAreaName,
                        WidthNum = s.widthNum
                    });
                }
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="isUse"></param>
        /// <returns>0:成功,1:更新数据库失败,2:没有找到对应的分区</returns>
        public int UpGridStatus(GridSubArea_Info grid, bool isUse)
        {
            int result = 0;// 操作成功
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                GridSubArea g = db.GridSubArea.FirstOrDefault(s => s.SubAreaID == grid.SubAreaID);
                if (g != null)
                {
                    g.isUseful = isUse;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {

                        result = 1;//更新数据库失败
                    }
                }
                else
                {
                    result = 2;// 没有找到对应的分区
                }
            }
            return result;
        }
    }
}
